package org.jadapter.tests;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;

import org.jadapter.JAdapter;
import org.jadapter.adapters.MapList;
import org.jadapter.registry.AdapterRegistry;
import org.jadapter.registry.TransitiveAdapterRegistry;
import org.jadapter.tests.examples.CommaSeparated;
import org.jadapter.tests.examples.CommaSeparatedList;

import junit.framework.TestCase;

@SuppressWarnings("unchecked")
public class TestAdapterRegistry extends TestCase {

    AdapterRegistry registry;
    JAdapter<CommaSeparated,List> listAdapter;
    List list;
    
    public void setUp(){
        registry = new TransitiveAdapterRegistry();
        list = Arrays.asList("foo", "bar");
    }
    
    public void testShouldAdaptImplementations(){
        listAdapter = new JAdapter<CommaSeparated,List>();
        listAdapter.setProvides(CommaSeparated.class);
        listAdapter.setFor(List.class);
        listAdapter.setAdapter(CommaSeparatedList.class);
        
        registry.register(listAdapter);
        CommaSeparated cs = registry.adapt(CommaSeparated.class, list);
        // list implements List so listAdapter should work
        assertNotNull(cs);
                
    }
    
    
    public void testShouldRegisterAndAdaptAdapterFactories(){
        registry.register(new MapList());
        SortedMap map = registry.adapt(SortedMap.class, list);
        
        assertTrue(map.containsKey(1));
        assertTrue(map.containsValue("foo"));
        
    }

    
    public void testShouldAdaptToSuperInterfaces(){
        registry.register(new MapList());
        Map map = registry.adapt(Map.class, list);
        // Map is a super interface of SortedMap, 
        // the registry should find that for us
        assertTrue(map.containsKey(1));
        assertTrue(map.containsValue("foo"));        
    }
}
